package backend

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyuadmin/api/backend"
	"miaoyuadmin/api/backend/response"
	"miaoyuadmin/internal/model"
	"miaoyuadmin/internal/service"
)

var UserDepartment = new(userDepartmentCtl)

type userDepartmentCtl struct {
}

func (c *userDepartmentCtl) Index(ctx context.Context, req *backend.UserDepartmentIndexReq) (res *backend.UserDepartmentIndexRes, err error) {
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl": "user/department/index.html",
		"footTpl": "user/department/index.js.html",
		"title":   "部门管理",
	})

	return
}
func (c *userDepartmentCtl) Add(ctx context.Context, req *backend.UserDepartmentAddReq) (res *backend.UserDepartmentAddRes, err error) {
	parentId := req.ParentId
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":  "user/department/add.html",
		"footTpl":  "user/department/add.js.html",
		"siteName": "控制台",
		"parentId": parentId,
	})
	return
}
func (c *userDepartmentCtl) Edit(ctx context.Context, req *backend.UserDepartmentEditReq) (res *backend.UserDepartmentEditRes, err error) {
	resId := req.ResId

	itemRes, resErr := service.UserDepartment().GetItemById(ctx, resId)
	if resErr != nil {
		err = resErr
		response.UniResp(g.RequestFromCtx(ctx), err.Error(), "")
		return
	}
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":  "user/department/add.html",
		"footTpl":  "user/department/edit.js.html",
		"siteName": "控制台",
		"data":     itemRes,
	})
	return
}

func (c *userDepartmentCtl) DelDo(ctx context.Context, req *backend.UserDepartmentDelDoReq) (res *backend.UserDepartmentDelDoRes, err error) {
	resId := req.ResId
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.UserDepartment().Del(ctx, resId)
	if resErr != nil {
		err = resErr
		commonJson.SetErrorJsonExit(err.Error())
		return
	}
	commonJson.SetSuccessJsonExit("操作成功", nil)
	return

}

func (c *userDepartmentCtl) ListDo(ctx context.Context, req *backend.UserDepartmentListDoReq) (res *backend.UserDepartmentListDoRes, err error) {

	commonJson := response.CommonResp(g.RequestFromCtx(ctx))

	listRes, resErr := service.UserDepartment().GetAllList(ctx, nil)
	if resErr != nil {
		err = resErr
		return
	}
	commonJson.SetSuccessJsonExit("ok", listRes)

	return
}
func (c *userDepartmentCtl) SubmitDo(ctx context.Context, req *backend.UserDepartmentSubmitDoReq) (res *backend.UserDepartmentSubmitDoRes, err error) {
	var submitInput model.UserDepartmentSubmitInput
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	err = gconv.Scan(req, &submitInput)
	if err != nil {
		return
	}
	resId := req.ResId
	if resId != 0 {
		_, err = service.UserDepartment().Edit(ctx, submitInput, resId)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		} else {
			commonJson.SetSuccessJsonExit("修改成功", nil)
		}
		return
	} else {
		_, err = service.UserDepartment().Add(ctx, submitInput)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		}

		commonJson.SetSuccessJsonExit("添加成功", nil)
	}

	return
}
